package com.sj.parksdk.adapter.carmanage;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ruffian.library.widget.RLinearLayout;
import com.ruffian.library.widget.RTextView;
import com.ruffian.library.widget.helper.RTextViewHelper;
import com.sj.parksdk.R;
import com.sj.parksdk.setting.Dictionary;
import com.xuexiang.xui.utils.ResUtils;

import java.util.List;

public class AdapterCarColorSelect extends RecyclerView.Adapter<AdapterCarColorSelect.ViewHolder>{

    private List<String> mData;

    private int selectedPosition = 0;

    private AdapterCarColorSelect.OnItemClickListener mOnItemClickListener;

    public List<String> getmData() {
        return mData;
    }

    public void setmData(List<String> mData) {
        this.mData = mData;
    }

    public int getSelectedPosition() {
        return selectedPosition;
    }

    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
    }

    public void setmOnItemClickListener(AdapterCarColorSelect.OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public AdapterCarColorSelect(List<String> mData) {
        this.mData = mData;
    }

    public String getItem(int index) {
        return mData.get(index);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View view =
                LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.parksdk_adapter_item_car_color, parent, false);
        return new ViewHolder(view,this);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.rtv.setText(Dictionary.Car.PlateColor.PlateColorNameMap.get(mData.get(position)));
        RTextViewHelper ivHelper = holder.iv.getHelper();
        if(selectedPosition == position){
            ivHelper.setBackgroundColorNormal(ResUtils.getColor(R.color.app_color_theme_main));
        }else{
            ivHelper.setBackgroundColorNormal(ResUtils.getColor(R.color.gray_btn_bg_color));
        }
        RTextViewHelper helper = holder.rtv.getHelper();
        if(Dictionary.Car.PlateColor.Blue.equals(mData.get(position))){
            helper.setBackgroundColorNormal(ResUtils.getColor(R.color.app_color_theme_main));
            holder.rll.getHelper().setBorderColorNormal(ResUtils.getColor(R.color.app_color_theme_main));
        }else if(Dictionary.Car.PlateColor.Yellow.equals(mData.get(position))){
            helper.setBackgroundColorNormal(ResUtils.getColor(R.color.color_tip_orange));
            holder.rll.getHelper().setBorderColorNormal(ResUtils.getColor(R.color.color_tip_orange));
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        RTextView iv ;
        RTextView rtv;
        RLinearLayout rll;
        AdapterCarColorSelect mAdapter;

        ViewHolder(@NonNull View itemView,AdapterCarColorSelect adapter) {
            super(itemView);
            rtv = itemView.findViewById(R.id.rtv);
            iv = itemView.findViewById(R.id.iv);
            rll = itemView.findViewById(R.id.rll_car_color);
            mAdapter = adapter;
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(mAdapter == null || mAdapter.mOnItemClickListener == null)return;
                    mAdapter.mOnItemClickListener.onItemSelected(getAdapterPosition(),mAdapter.getItem(getAdapterPosition()));
                }
            });
        }
    }

    public interface OnItemClickListener {

        void onItemSelected(int index, String item);
    }
}
